﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Borton.Beans;
using Borton.Data;

namespace Borton
{
    class GenericForm : Form
    {
        private Control control;
        private TableLayoutPanel table;
        private ToolStrip toolbar;
        private Bean bean;
        private bool readOnly;

        public GenericForm(Bean bean, bool readOnly) : base()
        {
            this.bean = bean;
            this.readOnly = readOnly;
            InitializeComponent();
            createControls();
        }

        private void createControls()
        {
            
            table.RowCount = bean.getFields().Length;
            this.Text = bean.getTableName();
            for (int i = 0; i < bean.getFields().Length; i++)
            {
                control = bean.getFields()[i].getControl(readOnly || bean.getId().Equals(bean.getFields()[i]));
                table.Controls.Add(ControlManager.createLabel(bean.getFields()[i]), 0, i);
                table.Controls.Add(control, 1, i);
            }
            ToolStripButton btn = ControlManager.createToolbarButton("Accept");
            btn.Click += new EventHandler(this.accept_Click);
            toolbar.Items.Add(btn);
            btn = ControlManager.createToolbarButton("Cancel");
            btn.Click += new EventHandler(this.cancel_Click);
            toolbar.Items.Add(btn);
        }

        private void accept_Click(object sender, EventArgs e)
        {
            bean.saveState();
            bean.save();
            this.Close();
        }

        private void cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(GenericForm));
            this.table = new System.Windows.Forms.TableLayoutPanel();
            this.toolbar = new System.Windows.Forms.ToolStrip();
            this.toolbar.SuspendLayout();
            this.SuspendLayout();
            // 
            // table
            // 
            this.table.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.table.AutoSize = true;
            this.table.ColumnCount = 2;
            this.table.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
            this.table.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
            this.table.Location = new System.Drawing.Point(12, 28);
            this.table.Name = "table";
            this.table.Size = new System.Drawing.Size(268, 10);
            this.table.TabIndex = 0;
            // 
            // toolbar
            // 
            this.toolbar.Location = new System.Drawing.Point(0, 0);
            this.toolbar.Name = "toolbar";
            this.toolbar.Size = new System.Drawing.Size(292, 25);
            this.toolbar.TabIndex = 1;
            this.toolbar.Text = "Toolbar";
            // 
            // GenericForm
            // 
            this.AutoSize = true;
            this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Controls.Add(this.toolbar);
            this.Controls.Add(this.table);
            this.Name = "GenericForm";
            this.toolbar.ResumeLayout(false);
            this.toolbar.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

    }
}
